package com.zzk.protect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzk.protect.common.CommonResult;
import com.zzk.protect.pojo.ConstantItem;
import com.zzk.protect.pojo.ConstantType;
import com.zzk.protect.service.ConstantItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/constantItem")
public class ConstantItemController {
    @Autowired
    private ConstantItemService constantItemService;
    @GetMapping("/list")
    public CommonResult<PageInfo<ConstantItem>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 正确使用PageHelper分页
        PageHelper.startPage(pageNum, pageSize);
        // 确保service中的list方法是基于MyBatis的查询，而不是简单获取所有数据
        List<ConstantItem> list = constantItemService.list(new QueryWrapper<ConstantItem>());
        PageInfo<ConstantItem> pageInfo = new PageInfo<>(list);
        return CommonResult.success(pageInfo);
    }

    @GetMapping("/select")
    public CommonResult<PageInfo<ConstantItem>> selectByType(
            @RequestParam Integer constantTypeId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<ConstantItem> wrapper = new QueryWrapper<>();
        wrapper.eq("constant_type_id", constantTypeId);
        List<ConstantItem> list = constantItemService.list(wrapper);
        PageInfo<ConstantItem> pageInfo = new PageInfo<>(list);
        return CommonResult.success(pageInfo);
    }
    @PostMapping("/add")
    public CommonResult add(@RequestBody ConstantItem constantItem){
        // 直接使用传入的对象，确保所有字段都能接收
        if (constantItem.getConstantTypeId() == null) {
            return CommonResult.failed("常数类别ID不能为空");
        }
        return CommonResult.success(constantItemService.save(constantItem));
    }
    //批量删除
    @DeleteMapping("/delete")
    public CommonResult<Boolean> deleteBatch(@RequestBody ConstantItem constantItem){
       QueryWrapper<ConstantItem> wrapper = new QueryWrapper<>();
       wrapper.eq("id",constantItem.getId());
        return CommonResult.success(constantItemService.remove(wrapper));
    }
    @PatchMapping("/update/")
    public CommonResult updateById(@RequestBody ConstantItem constantItem){
        // 检查code是否已存在（排除自己）
        QueryWrapper<ConstantItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", constantItem.getCode());
        queryWrapper.eq("constant_type_id", constantItem.getConstantTypeId());
        queryWrapper.ne("id", constantItem.getId());
        if (constantItemService.count(queryWrapper) > 0) {
            return CommonResult.failed("编码已存在");
        }


        return CommonResult.success( constantItemService.updateById(constantItem));
    }
    @GetMapping("/getByTypeId")
    public CommonResult<List<ConstantItem>> getByTypeId(@RequestParam Integer id){
        QueryWrapper<ConstantItem> qw = new QueryWrapper<>();
        qw.eq("constant_type_id",id);
        return CommonResult.success(constantItemService.list(qw));
    }
//    @GetMapping("/selectOrName")
//    public CommonResult selectByIdOrName(@RequestBody  Object param) {
//        try {
//            List<ConstantItem> result = constantItemService.selectByIdOrName(param);
//            return CommonResult.success(result);
//        } catch (Exception e) {
//            return CommonResult.failed("查询失败: " + e.getMessage());
//        }
//    }
}
